#include<iostream>
using namespace std;
const int N = 3e4 + 10;
int p[N], s[N], d[N];
int m;
int find(int x)
{
	if (x != p[x]) {
		int root = find(p[x]);
		d[x] += d[p[x]];
		p[x] = root;
	}
	return p[x];
}
int main()
{
	for (int i = 1; i < N; i++) {
		p[i] = i;
		s[i] = 1;
	}
	cin >> m;
	while (m--) {
		char op[2];
		int a, b;
		cin >> op >> a >> b;
		if (op[0] == 'M') {
			a = find(a);
			b = find(b);
			if (a != b) {
				d[a] = s[b];
				s[b] += s[a];
				p[a] = b;
			}
		}
		else {
			int pa = find(a);
			int pb = find(b);
			if (pa != pb) {
				cout << "-1" << endl;
			}
			else
			    cout << max(0, abs(d[a] - d[b]) - 1) << endl;
		}
	}
	return 0;
}